﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.SqlClient;
using System.Data;
using DAL;
using DevExpress.Web.ASPxGridView;
using System.Text;
using DevExpress.Web.ASPxEditors;

namespace EDMS.Admin.DelegateRecord
{
    public partial class RecordAccepts : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                //判断用户是否已经登录
                try
                {
                    if (!CMSUser.Current.IsLogin)
                    {
                        Response.Redirect("~/Login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("~/Login.aspx");
                }
                databind();
                wtSearch();
                bwtSearch();
            }
        }
        void databind()
        {
            using (DataClassesDataContext lqDB = new DataClassesDataContext())
            {
                //绑定数据
                string jlxh = Request.QueryString["id"].ToString();
                string CurrentXjbh = Session["Xjbh"].ToString();
                string strsql = "";
                if (CMSUser.Current.IsAdmin == true)
                {
                    strsql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl where jlbh='" + jlxh + "' order by fssj desc ";
                }
                else
                {
                    strsql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl where xjbh='" + CurrentXjbh + "' and jlbh='" + jlxh + "' order by fssj desc ";
                }

                DataTable table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strsql);
                gvShow.DataSource = table;
                gvShow.KeyFieldName = "jlbh";
                gvShow.DataBind();
                //文本框只读设置
                txtReadOnly();
               
                #region//将数据显示在文本框内
                var record = lqDB.Wtjl.FirstOrDefault(r => r.jlbh == jlxh);
                if (CMSUser.Current.IsAdmin == true)
                {
                     record = lqDB.Wtjl.FirstOrDefault(r => r.jlbh == jlxh);
                }
                else
                {
                     record = lqDB.Wtjl.FirstOrDefault(r => r.jlbh == jlxh && r.xjbh == CurrentXjbh);
                }
                
                //根据选举编号查处选举名称 
                string xjbho = record.xjbh;
                var XMo = lqDB.Xjdj.FirstOrDefault(m => m.xjbh == xjbho);
                string xjmc = XMo.xjmc;

                txtjlbh.Text = jlxh;                    //记录编号

                //选举名称和编号
                cmdxjmc.DataSourceID = "";
                cmdxjmc.Items.Clear();
                ListEditItem list = new ListEditItem();
                list.Text = xjmc + "(编号：" + record.xjbh + ")";
                list.Value = record.xjbh;
                cmdxjmc.Items.Add(list);
                cmdxjmc.SelectedIndex = 1;
                
                //txtxjbh.Text = xjmc + "(编号：" + xjbho + ")";             //选举名称(编号)

                txtwtname.Text = record.wtname;         //委托人姓名      *
                txtbwtname.Text = record.bwtname;       //被委托人姓名    *
                txtwtsf.Text = record.wtrsfzh;          //委托人身份证    *
                txtbwtsf.Text = record.bwtrsfzh;        //被委托人身份证   *

                txtxmxms.Text = record.wtname;
                txtxmcards.Text = record.wtrsfzh;
                txtxmxmb.Text = record.bwtname;
                txtxmcardb.Text = record.bwtrsfzh;

                txtip.Text = record.ip;                        //委托人电脑IP
                cmdprint.Date = Convert.ToDateTime(record.wtsdysj);     //委托书打印时间
                lblyzm.Text = record.yzm;                      //验证码
                txtfssj.Text = record.fssj.ToString();         //委托发生时间
                string wtzt = isWtzt(record.flag);
                txtflag.Text = wtzt;                           //委托状态  
                string wtfs = isWtfs(record.wtfs);
                txtwtfs.Text = wtfs;                           //委托方式
                #endregion

                if (wtzt == "成功" || wtzt == "失败")    //判断委托是否成功 待定则进行委托管理
                {
                    btnOK.Visible = false;
                    btnNo.Visible = false;
                }

                if (record.issl == '1')  //判断是否受理成功或失败
                {
                    //进行受理操作
                    txtczr.Text = CMSUser.Current.Name;      //当前操作人
                    txttime.Text = DateTime.Now.ToString();  //当前日期
                }
                else
                {
                    imgwtEdits.Visible = false;
                    imgBwtEdit.Visible = false;
                    //如果已受理显示受理工作记录
                    var rSljl = lqDB.Sljl.FirstOrDefault(s => s.wtjlbh == jlxh);
                    if (rSljl != null)
                    {
                        txtsm.Text = rSljl.sm;
                        txttime.Text = rSljl.fssj.ToString();
                        txtczr.Text = rSljl.czr;
                    }
                }
            }
        }
        //委托方式判断
        string isWtfs(string fs)
        {
            string wtfs = "";
            switch (fs)
            {
                case "D":
                    wtfs = "电话委托";
                    break;
                case "X":
                    wtfs = "短信委托";
                    break;
                case "W":
                    wtfs = "网站委托";
                    break;
                default:
                    break;
            }
            return wtfs;
        }
        //委托状态判断
        string isWtzt(char zt)
        {
            string wtzt = "";
            switch (zt)
            {
                case '9':
                    wtzt = "成功";
                    break;
                case '0':
                    wtzt = "失败";
                    break;
                case '1':
                    wtzt = "待定";
                    break;
            }
            return wtzt;
        }

        //窗体加载时文本框只读设置
        void txtReadOnly()
        {
            #region 文本框只读设置
           // txtxjbh.ReadOnly = true;
            txtjlbh.ReadOnly = true;
            txtwtfs.ReadOnly = true;
            txtflag.ReadOnly = true;
            txtip.ReadOnly = true;
            //cmdprint.ReadOnly = true;
            lblyzm.ReadOnly = true;
            txtfssj.ReadOnly = true;
            txttime.ReadOnly = true;
            txtczr.ReadOnly = true;
            #endregion
        }
        //委托成功失败操作
        void wtCommend(char wtflag, char issl)
        {
            string jlxh = Request.QueryString["id"].ToString();
            string wtname = txtwtname.Text;     //委托人姓名
            string bwtname = txtbwtname.Text;   //被委托人姓名
            string wtidcard = txtwtsf.Text;     //委托人身份证号码
            string bwtidcard = txtbwtsf.Text;   //被委托人身份证号码

            using (DataClassesDataContext lqDB = new DataClassesDataContext())
            {
                Wtjl w = lqDB.Wtjl.FirstOrDefault(r => r.jlbh == txtjlbh.Text);
                w.wtname = wtname;
                w.wtrsfzh = wtidcard;
                w.bwtname = bwtname;
                w.bwtrsfzh = bwtidcard;
                w.flag = wtflag;
                w.issl = issl;                //标记为已经受理
                lqDB.SubmitChanges();
            }
        }
        //插入受理记录说明
        void InsertSljl()
        {
            string jlxh = Request.QueryString["id"].ToString();
            using (DataClassesDataContext lqdb = new DataClassesDataContext())
            {
                var result = lqdb.Sljl.FirstOrDefault(r => r.wtjlbh == jlxh);
                if (result != null)
                {
                    result.sm = txtsm.Text;
                    //result.fssj = Convert.ToDateTime(txttime.Text);
                    result.fssj = DateTime.Now;
                    result.czr = txtczr.Text;
                    result.wtjlbh = jlxh;
                    lqdb.SubmitChanges();
                }
                else
                {
                    Sljl s = new Sljl();
                    s.sm = txtsm.Text;
                    s.fssj = Convert.ToDateTime(txttime.Text);
                    s.czr = txtczr.Text;
                    s.wtjlbh = jlxh;
                    lqdb.Sljl.InsertOnSubmit(s);
                    lqdb.SubmitChanges();
                }


            }
        }
        //确认委托
        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!txtsm.Text.Equals(""))
            {
                char wtflag = '9';                  //委托状态 成功
                char issl = '9';                  //受理成功
                wtCommend(wtflag, issl);
                //插入受理记录说明
                InsertSljl();
                Response.Write("<script language='javascript'>alert('委托成功！');window.close()</script> } ");
            }
            else
            {
                Frame.Global.GlobalFunctions.Alert("请输入您的工作记录！");
            }

        }
        //委托失败
        protected void btnNo_Click(object sender, EventArgs e)
        {
            if (!txtsm.Text.Equals(""))
            {
                char wtflag = '0';                //委托状态 失败
                char issl = '0';                  //受理失败
                wtCommend(wtflag, issl);
                InsertSljl();
                Response.Write("<script language='javascript'>alert('委托失败！');window.close()</script> } ");


            }
            else
            {
                Frame.Global.GlobalFunctions.Alert("请输入您的工作记录！");
            }
        }
        void wtSearch()
        {
            string wtname = txtwtname.Text;              //委托人姓名
            string wtidcard = txtwtsf.Text;              //委托人身份证号码
            string CurrentXjbh = Session["Xjbh"].ToString();
            string strSql = "";
            if (CMSUser.Current.IsAdmin == true)
            {
                if (!string.IsNullOrEmpty(wtidcard))
                {
                    strSql = "select * from Xm where sfzh like '%" + wtidcard + "%'";
                }
                if (!string.IsNullOrEmpty(wtname))
                {
                    strSql = "select * from Xm where xmxm like '%" + wtname + "%'";
                }
                if (!string.IsNullOrEmpty(wtidcard) && !string.IsNullOrEmpty(wtname))
                {
                    strSql = "select * from Xm where xmxm like '%" + wtname + "%' and sfzh like '%" + wtidcard + "%'";
                }

                if (string.IsNullOrEmpty(wtidcard) && string.IsNullOrEmpty(wtname))
                {
                    strSql = "select * from Xm where xmxm='xxxx'";
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(wtidcard))
                {
                    strSql = "select * from Xm where sfzh like '%" + wtidcard + "%' and xjbh='" + CurrentXjbh + "'";
                }
                if (!string.IsNullOrEmpty(wtname))
                {
                    strSql = "select * from Xm where xmxm like '%" + wtname + "%' and xjbh='" + CurrentXjbh + "'";
                }
                if (!string.IsNullOrEmpty(wtidcard) && !string.IsNullOrEmpty(wtname))
                {
                    strSql = "select * from Xm where xmxm like '%" + wtname + "%' and sfzh like '%" + wtidcard + "%' and xjbh='" + CurrentXjbh + "'";
                }

                if (string.IsNullOrEmpty(wtidcard) && string.IsNullOrEmpty(wtname))
                {
                    strSql = "select * from Xm where xmxm='xxxx' and xjbh='" + CurrentXjbh + "'";
                }
            }

            DataTable dt = new DataTable();
            dt = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                gvwtShow.DataSource = dt;
                gvwtShow.KeyFieldName = "sfzh";
                gvwtShow.DataBind();
                lblmsg.Visible = false;
            }
            else
            {
                lblmsg.Text = "没找到你需要的信息";
            }
        }

        void bwtSearch()
        {
            string wtname = txtbwtname.Text;              //被委托人姓名
            string wtidcard = txtbwtsf.Text;              //被委托人身份证号码
            string CurrentXjbh = Session["Xjbh"].ToString();
            string strSql = "";
            if (CMSUser.Current.IsAdmin == true)
            {
                if (!string.IsNullOrEmpty(wtidcard))
                {
                    strSql = "select * from Xm where sfzh like '%" + wtidcard + "%'";
                }
                if (!string.IsNullOrEmpty(wtname))
                {
                    strSql = "select * from Xm where xmxm like '%" + wtname + "%'";
                }
                if (!string.IsNullOrEmpty(wtidcard) && !string.IsNullOrEmpty(wtname))
                {
                    strSql = "select * from Xm where xmxm like '%" + wtname + "%' and sfzh like '%" + wtidcard + "%'";
                }
                if (string.IsNullOrEmpty(wtidcard) && string.IsNullOrEmpty(wtname))
                {
                    strSql = "select * from Xm where xmxm='xxxx'";
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(wtidcard))
                {
                    strSql = "select * from Xm where sfzh like '%" + wtidcard + "%' and xjbh='" + CurrentXjbh + "'";
                }
                if (!string.IsNullOrEmpty(wtname))
                {
                    strSql = "select * from Xm where xmxm like '%" + wtname + "%' and xjbh='" + CurrentXjbh + "'";
                }
                if (!string.IsNullOrEmpty(wtidcard) && !string.IsNullOrEmpty(wtname))
                {
                    strSql = "select * from Xm where xmxm like '%" + wtname + "%' and sfzh like '%" + wtidcard + "%' and xjbh='" + CurrentXjbh + "'";
                }
                if (string.IsNullOrEmpty(wtidcard) && string.IsNullOrEmpty(wtname))
                {
                    strSql = "select * from Xm where xmxm='xxxx'";
                }
            }

            DataTable dt = new DataTable();
            dt = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                gvbwtShow.DataSource = dt;
                gvbwtShow.KeyFieldName = "sfzh";
                gvbwtShow.DataBind();
                lblmsg1.Visible = false;
            }
            else
            {
                lblmsg1.Text = "没找到你需要的信息";
            }
        }

        //委托人查询
        protected void gvwtShow_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            string str = e.Parameters;
            string wtnametext = txtxmxms.Text;
            string wtidcardtext = txtxmcards.Text;
            string CurrentXjbh = Session["Xjbh"].ToString();
            string strSql = "";
            if (CMSUser.Current.IsAdmin == true)
            {
                strSql = "select * from Xm where {0} 1=1";
            }
            else
            {
                strSql = "select * from Xm where {0} xjbh='" + CurrentXjbh + "'";
            }

            StringBuilder sb = new StringBuilder();
            List<SqlParameter> paralist = new List<SqlParameter>();
            if (!string.IsNullOrEmpty(wtnametext))
            {
                sb.Append("xmxm like '%'+ @name +'%' and ");
                paralist.Add(new SqlParameter("@name", wtnametext));
            }
            if (!string.IsNullOrEmpty(wtidcardtext))
            {
                sb.Append("sfzh like '%'+ @idcard +'%' and ");
                paralist.Add(new SqlParameter("@idcard", wtidcardtext));
            }
            string strsql = string.Format(strSql, sb.ToString());
            if (string.IsNullOrEmpty(wtnametext) && string.IsNullOrEmpty(wtidcardtext))
            {
                strsql = "select * from Xm where xmxm='xxxxxxxxxxxxxxx'";
            }
            gvwtShow.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strsql, paralist.ToArray());
            gvwtShow.DataBind();
        }

        //委托
        protected void btnClearwt_Click(object sender, EventArgs e)
        {
            if (!txtxmxms.Text.Equals("") && !txtxmcards.Text.Equals(""))
            {
                txtwtname.Text = txtxmxms.Text;
                txtwtsf.Text = txtxmcards.Text;
                //txtxmxms.Text = "";
                //txtxmcards.Text = "";
            }

        }
        //被委托
        protected void btnClear_Click(object sender, EventArgs e)
        {
            if (!txtxmxmb.Text.Equals("") && !txtxmcardb.Text.Equals(""))
            {
                txtbwtname.Text = txtxmxmb.Text;
                txtbwtsf.Text = txtxmcardb.Text;
                //txtxmcardb.Text = "";
                //txtxmxmb.Text = "";
            }
        }
        // 被委托查询
        protected void gvbwtShow_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            string str = e.Parameters;
            string wtnametext = txtxmxmb.Text;
            string wtidcardtext = txtxmcardb.Text;
            string CurrentXjbh = Session["Xjbh"].ToString();
            string strSql = "";
            if (CMSUser.Current.IsAdmin == true)
            {
                strSql = "select * from Xm where {0} 1=1";
            }
            else
            {
                strSql = "select * from Xm where {0} xjbh='" + CurrentXjbh + "'";
            }
            StringBuilder sb = new StringBuilder();
            List<SqlParameter> paralist = new List<SqlParameter>();

            if (!string.IsNullOrEmpty(wtnametext))
            {
                sb.Append("xmxm like '%'+ @name +'%' and ");
                paralist.Add(new SqlParameter("@name", wtnametext));
            }

            if (!string.IsNullOrEmpty(wtidcardtext))
            {
                sb.Append("sfzh like '%'+ @idcard +'%' and ");
                paralist.Add(new SqlParameter("@idcard", wtidcardtext));
            }
            string strsql = string.Format(strSql, sb.ToString());
            if (string.IsNullOrEmpty(wtnametext) && string.IsNullOrEmpty(wtidcardtext))
            {
                strsql = "select * from Xm where xmxm='xxxxxxxxxxxxxx'";
            }
            gvbwtShow.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strsql, paralist.ToArray());
            gvbwtShow.DataBind();
        }




    }
}